package controllers

import (
	"2302a_wz/cache"
	"2302a_wz/models"
	"encoding/json"
	"fmt"
	beego "github.com/beego/beego/v2/server/web"
)

type WzController struct {
	beego.Controller
}

func (c *WzController) Create() {
	var err error
	title := c.GetString("title")
	name := c.GetString("name")
	typeId, _ := c.GetInt("type_id")
	writer := c.GetString("writer")
	like := c.GetString("like")
	status := c.GetString("status")

	if title == "" || name == "" || typeId == 0 || writer == "" || like == "" || status == "" {
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"mes":  "输入内容不能为空",
		}
		c.ServeJSON()
		return
	}
	wzs := models.Wz{
		Title:  title,
		Name:   name,
		TypeId: typeId,
		Write:  writer,
		Like:   like,
		Status: status,
	}
	if err = wzs.Create(); err != nil {
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"mes":  "系统错误",
		}
		c.ServeJSON()
		return
	}
	key := fmt.Sprintf("wz_id_%d", wzs.ID)
	jsondate, _ := json.Marshal(&wzs)
	cache.Rdb.Set(cache.Ctx, key, jsondate, 0)
	c.Data["json"] = map[string]interface{}{
		"code": 200,
		"mes":  "添加成功",
	}
	c.ServeJSON()
	return

}
func (c *WzController) List() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	keyword := c.GetString("keyword")
	TypeID, _ := c.GetInt("type_id")
	page, _ := c.GetInt("page")
	pageSize, _ := c.GetInt("pageSize")

	key := fmt.Sprintf("wz_list_%s_%d_%d", keyword, TypeID, page)
	cacheDta, _ := cache.Rdb.Get(cache.Ctx, key).Result()

	var wzs models.Wz
	var list []models.Wz

	if cacheDta != "" {
		json.Unmarshal([]byte(cacheDta), &list)
	} else {
		list, _ := wzs.List(keyword, TypeID, page, pageSize)
		if len(list) != 0 {
			JsonData, _ := json.Marshal(&list)
			cache.Rdb.Set(cache.Ctx, key, JsonData, 0)
		}
	}
	c.Data["json"] = map[string]interface{}{
		"code": 200,
		"data": list,
		"mes":  "展示成功",
	}
	c.ServeJSON()
	return
}
func (c *WzController) Update() {
	var err error
	id, _ := c.GetInt("id")
	title := c.GetString("title")
	name := c.GetString("name")
	typeId, _ := c.GetInt("type_id")
	writer := c.GetString("writer")
	like := c.GetString("like")
	status := c.GetString("status")

	if title == "" || name == "" || typeId == 0 || writer == "" || like == "" || status == "" {
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"mes":  "输入内容不能为空",
		}
		c.ServeJSON()
		return
	}
	wzs := models.Wz{
		Title:  title,
		Name:   name,
		TypeId: typeId,
		Write:  writer,
		Like:   like,
		Status: status,
	}
	wzs.ID = uint(id)
	if err = wzs.Update(); err != nil {
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"mes":  "系统错误",
		}
		c.ServeJSON()
		return
	}
	key := fmt.Sprintf("wz_id_%d", wzs.ID)
	jsondate, _ := json.Marshal(&wzs)
	cache.Rdb.Set(cache.Ctx, key, jsondate, 0)
	c.Data["json"] = map[string]interface{}{
		"code": 200,
		"mes":  "修改成功",
	}
	c.ServeJSON()
	return
}
func (c *WzController) Del() {
	var err error
	id, _ := c.GetInt("id")
	var wzs models.Wz
	wzs.ID = uint(id)
	if err = wzs.Del(); err != nil {
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"mes":  "系统错误",
		}
		c.ServeJSON()
		return
	}
	key := fmt.Sprintf("wz_id_%d", wzs.ID)
	cache.Rdb.Del(cache.Ctx, key)
	c.Data["json"] = map[string]interface{}{
		"code": 200,
		"mes":  "删除成功",
	}
	c.ServeJSON()
	return
}
